package com.hardy.controller;

import com.hardy.pojo.Resume;
import com.hardy.service.ResumeService;
import com.hardy.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @Author: HardyYao
 * @Date: 2020/5/31
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    public final static String USER_INFO = "userInfo";

    public final static String SESSION = "session:";

    public final static String ContextNameSpace = "mvc-data:";

    public final static String REDIS_SESSION_ID = "redisSessionId";

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //查询所有
    @RequestMapping("/queryAll")
    public ModelAndView queryAll() {
        //查询Resume列表
        List<Resume> resumes = resumeService.findAll();
        // 封装了数据和页面信息的 ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // addObject 其实是向请求域中request.setAttribute("resumes",resumes);
        modelAndView.addObject("resumes", resumes);
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("resumeList");
        return modelAndView;
    }

    @RequestMapping("/toAddResume")
    public ModelAndView toAddResume() {
        ModelAndView modelAndView = new ModelAndView();
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("addResume");
        return modelAndView;
    }

    @RequestMapping("/addResume")
    public ModelAndView addResume(@RequestParam String name, @RequestParam String address, @RequestParam String phone) {

        Resume resume = new Resume();
        resume.setName(name);
        resume.setAddress(address);
        resume.setPhone(phone);
        //保存resume
        resumeService.addResume(resume);
        ModelAndView modelAndView = new ModelAndView();
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("addResume");
        return modelAndView;

    }

    @RequestMapping("/deleteResume")
    public ModelAndView deleteResume(@RequestParam Long resumeId) {
        //删除Resume
        resumeService.deleteResume(resumeId);
        return this.queryAll();
    }

    @RequestMapping("/updateResume")
    public ModelAndView updateResume(@RequestParam Long resumeId, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Resume resume = new Resume();
        //通过ID获取resume
        resume = resumeService.getResumeById(resumeId);
        ModelAndView modelAndView = new ModelAndView();
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.addObject("resume", resume);
        modelAndView.setViewName("updateResume");
        return modelAndView;
    }

    @RequestMapping("/updateByResume")
    public ModelAndView updateByResume(@RequestParam String name, @RequestParam Long resumeId, @RequestParam String address, @RequestParam String phone) {
        Resume resume = new Resume();
        resume.setId(resumeId);
        resume.setName(name);
        resume.setAddress(address);
        resume.setPhone(phone);
        //修改resume，有ID值的话就是修改
        resumeService.addResume(resume);
        return this.queryAll();
    }


    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest req, HttpServletResponse resp, String username, String password) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        String redisSessionKey = CookieUtil.getCookie(REDIS_SESSION_ID, req);
        String sessionIdValue = UUID.randomUUID().toString();
        CookieUtil.setCookie(REDIS_SESSION_ID, sessionIdValue, -1, "/", resp);
        if("admin".equals(username) && "admin".equals(password)){
            HttpSession session = req.getSession();
            session.setAttribute("loginUserId", username);
            String sessionId = session.getId();
            redisTemplate.opsForValue().set(username, sessionId);
            //查询Resume列表
            List<Resume> resumes = resumeService.findAll();
            // 封装了数据和页面信息的 ModelAndView
            // addObject 其实是向请求域中request.setAttribute("resumes",resumes);
            modelAndView.addObject("resumes", resumes);
            // 视图信息(封装跳转的页面信息) 逻辑视图名
            modelAndView.setViewName("resumeList");
            return modelAndView;
        }else{
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }


    public ModelAndView queryAll(ModelAndView modelAndView) {
        //查询Resume列表
        List<Resume> resumes = resumeService.findAll();
        // 封装了数据和页面信息的 ModelAndView
        // addObject 其实是向请求域中request.setAttribute("resumes",resumes);
        modelAndView.addObject("resumes", resumes);
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("resumeList.jsp");
        return modelAndView;
    }
}
